package com.yh.jandan.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Page;
import com.yh.jandan.ext.LocalApp;
import com.yh.jandan.ext.thread.MeiziCheckThread;
import com.yh.jandan.ext.thread.MeiziSaveThread;
import com.yh.jandan.model.Config;
import com.yh.jandan.model.Meizi;
import com.yh.jandan.util.DateUtils;

public class JandanController extends Controller {
	public static final String key = "__PSWD__";
	public static boolean autocatch = true;
	
	private static int PAGESIZE = 10;
	
	//用图片URL或MD5值可以查询图片信息
	public void search ( ) {
		Map < String , Object > msg = new HashMap < String , Object > ( );
		String val = getPara ( "val" , "val" ).trim() ;
		Meizi meizi = Meizi.dao.findFirst ( "select * from meizi where `show`=? and (md5=? or big=?)" , "true" , val, val );
		if ( meizi == null ) { meizi= new Meizi();  msg.put("code","1"); }
		else { msg.put("code","0"); }

		msg.put("meizi",meizi);
		renderJson ( msg );		
	}

	public void watch ( ) {
		List < String > jsfilelist = get_js_file ( new Date ( ) ); 
		setAttr ( "jsfilelist" , JsonKit.toJson ( jsfilelist ) );
	}
	
	public void pool ( ) {
		Map < String , String > msg = new HashMap < String , String > ( );
		int size = MeiziSaveThread.getCurrentLength ( ) + MeiziCheckThread.getCurrentLength ( );
		msg.put ( "size" , size + "" );
		renderJson ( msg );
		return;
	}
	
	public void index ( ) {
		String page = getPara ( "page" , "1" );
		Date date = new Date ( );
		Page < Meizi > meizis = Meizi.dao.paginate ( Integer.parseInt ( page ) , PAGESIZE ,//
				"select *" , "from meizi where `show`=? and createdate<? order by id desc" , "true" , date );
		setAttr ( "longtime" , date.getTime ( ) );
		setAttr ( "meizi" , meizis );
	}
	
	// 提交需要删除的图片
	public void remove ( ) {
		String md5 = getPara ( "md5" , getPara ( 0 , "md5" ) );
		Meizi meizi = Meizi.dao.findFirst ( "select * from meizi where `show`=? and md5=?" , "true" , md5 );
		if ( meizi != null ) { // 图片存在
			if ( MeiziCheckThread.getCurrentLength ( ) < 100 ) {
				MeiziCheckThread.hold ( meizi );
			}
		}
		renderJson ( "0" );
		
	}
	
	public void logout ( ) {
		removeSessionAttr ( key );
		getSession ( ).invalidate ( );
		redirect301 ( "/" );
	}
	
	public void login ( ) {
		String pswd = getPara ( "pswd" , getPara ( 0 , "" ) );
		removeSessionAttr ( key );
		if ( null == pswd || pswd.equals ( "" ) ) {
			getSession ( ).invalidate ( );
			redirect301 ( "/login.jsp" );
			return;
		}
		Map < String , String > msg = new HashMap < String , String > ( );
		msg.put ( "retcode" , "-1" );
		Config config = new Config ( ).findFirst ( "select * from config where name=?" , "pswd" );
		if ( null == config || !config.getVal ( ).equals ( pswd ) ) {
			msg.put ( "retmsg" , "pswd error" );
		} else {
			setSessionAttr ( key , key );
			msg.put ( "retcode" , "0" );
		}
		renderJson ( msg );
		return;
	}
	
	public void del ( ) {
		Date date = new Date ( );
		Page < Meizi > meizis = Meizi.dao.paginate ( getParaToInt ( "page" , 1 ) , 100 ,//
				"select * " , "from meizi where `show`=? and createdate<?  order by id desc " , "false" , new Date ( ) );
		setAttr ( "longtime" , date.getTime ( ) );
		setAttr ( "meizi" , meizis );
		render ( "/del.jsp" );
	}
	
	public void _catch ( ) {
		Map < String , String > msg = new HashMap < String , String > ( );
		if ( !autocatch ) LocalApp.getLocalApp ( ).start ( );
		msg.put ( "retcode" , "0" );
		renderJson ( msg );
		return;
	}
	
	public void _more ( ) {
		Map < String , Object > msg = new HashMap < String , Object > ( );
		String page = getPara ( "page" , "1" );
		String longtime = getPara ( "longtime" , System.currentTimeMillis ( ) + "" );
		Date date = getDate ( Long.valueOf ( longtime ) );
		Page < Meizi > meizis = Meizi.dao.paginate ( Integer.parseInt ( page ) , PAGESIZE ,//
				"select * " , "from meizi where `show`=? and createdate<? order by id desc" , "true" , date );
		msg.put ( "longtime" , longtime );
		msg.put ( "meizis" , meizis );
		msg.put ( "retcode" , "0" );
		renderJson ( msg );
		return;
	}
	
	public void _hasNew ( ) {
		Map < String , Object > msg = new HashMap < String , Object > ( );
		String longtime = getPara ( "longtime" , System.currentTimeMillis ( ) + "" );
		Date date = getDate ( Long.valueOf ( longtime ) );
		List < Meizi > l = new Meizi ( ).find ( "select createdate from meizi where `show`=? and createdate>?" , "true" , date );
		msg.put ( "retcode" , ( l == null ? 0 : l.size ( ) ) );
		renderJson ( msg );
	}
	
	private List < String > get_js_file ( Date date ) {
		List < String > dateList = new ArrayList < String > ( );
		int i = 0;
		Date tmpdate = date;
		while ( i < 100 ) {
			if(dateList.size ( )>30 ) break;
			String daystr = DateUtils.getDateByFormat ( tmpdate , DateUtils.getFormat ( "" ) );
			File f = new File ( PathKit.getWebRootPath ( ) + File.separator + "watch" , daystr + ".js" );
			if ( f.exists ( ) ) {
				dateList.add ( daystr );
			}
			i++;
			tmpdate = DateUtils.addDay ( tmpdate , -1 );
		}
		return dateList;
	}
	
	private String to_date_str ( Date date ) {
		return DateUtils.getDateByFormat ( date , DateUtils.YMDHMS_FORMAT );
	}
	
	private Date getDate ( Long date ) {
		return new Date ( date );
	}
	
	public static void setPAGESIZE ( int pagesize ) {
		PAGESIZE = pagesize;
	}
	
	static {
		setPAGESIZE ( 6 );
	}
	
	public static void setAutocatch ( boolean autocatch ) {
		JandanController.autocatch = autocatch;
	}
}
